//
//  LAWebViewController.m
//  lingAo
//
//  Created by saxon on 16/9/20.
//  Copyright © 2016年 saxon. All rights reserved.
//

#import "LAWebViewController.h"
#import "UIWebView+JavaScript.h"
#import "LALoginViewController.h"

@interface LAWebViewController () <UIWebViewDelegate, UIWebViewJavaScriptDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) UIBarButtonItem *backItem;

@property (nonatomic, strong) NSString *url;
@end

@implementation LAWebViewController

- (instancetype)initWithUrl:(NSString *)url
{
    if (self = [super init]) {
        self.url = url;
    }
    return self;
}
- (void)viewDidLoad {
    NSString *cookieStr = [USER_DEFAULT_t objectForKey:@"cookie"];
    if (cookieStr) {
        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
        [cookieProperties setObject:@"mobilehousehold"        forKey:NSHTTPCookieName];
        [cookieProperties setObject:cookieStr forKey:NSHTTPCookieValue];
        [cookieProperties setObject:HOST forKey:NSHTTPCookieDomain];
        [cookieProperties setObject:HOST forKey:NSHTTPCookieOriginURL];
        [cookieProperties setObject:@"/"                forKey:NSHTTPCookiePath];
        [cookieProperties setObject:@"0"                forKey:NSHTTPCookieVersion];
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
    self.webView.jsDelegate = self;
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = HTMLCOLOR(@"#2e3339");
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:BOLDFONT(18)};
    
    self.title = @"凌奥人口";
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}
- (NSString *)url
{
    if (!_url) {
        _url = [NSString stringWithFormat:@"http://%@/mobileweb/news", HOST];
    }
    return _url;
}
- (UIBarButtonItem *)backItem
{
    if (!_backItem) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.frame = CGRectMake(0, 0, 48, 43);
        [backBtn setImage:[self creatBackItem:NO] forState:UIControlStateNormal];
        [backBtn setImage:[self creatBackItem:YES] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(processBack:) forControlEvents:UIControlEventTouchUpInside];
        _backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    }
    return _backItem;
}
- (UIImage *)creatBackItem:(BOOL)isHigh
{
    UIView *backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, 0, 48, 43);
    UIImage *image = isHigh ? [IMAGE(@"backicon") imageWithColor:[UIColor grayColor]] : IMAGE(@"backicon");
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 18)];
    imageView.center = CGPointMake( image.size.width / 2, backView.bounds.size.height / 2 );
    imageView.image = image;
    [backView addSubview:imageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(image.size.width + 3, 0,
                                                              backView.bounds.size.width - image.size.width + 5, backView.bounds.size.height)];
    label.text = @"返回";
    label.textColor = isHigh ? [UIColor grayColor] : [UIColor whiteColor];
    label.font = FONT(16);
    [backView addSubview:label];
    
    
    return [RTToolObject convertViewToImage:backView];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *url = [request.URL.absoluteString componentsSeparatedByString:@"?"].firstObject;
    
    
    NSLog(@"%@---%d", url, [webView canGoBack]);
    if (![url isEqualToString:[NSString stringWithFormat:@"http://%@/mobileweb/news", HOST]] && self.navigationController.viewControllers.count==1) {
        self.navigationItem.leftBarButtonItem = self.backItem;
    } else {
        self.navigationItem.leftBarButtonItem = nil;
    }
    
    if ([url isEqualToString:[NSString stringWithFormat:@"http://%@/mobileweb/login", HOST]]) {
        [self presentViewController:[[LALoginViewController alloc] init] animated:NO completion:nil];
        self.navigationItem.leftBarButtonItem = nil;
    }
    return YES;
}
- (void)processBack:(id)sender
{
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }
}
- (void)pushToLoginViewController:(UIViewController *)vc
{
    [RTHttpClient Post:[NSString stringWithFormat:@"http://%@/mobileweb/index/addEditToken", HOST] data:@{@"type":@"ios", @"token":@"12345",@"cookie":[[NSUserDefaults standardUserDefaults] objectForKey:@"cookie"]} success:^(NSDictionary *data) {
    } fail:^(NSString *error) {
    }];
    [USER_DEFAULT_t removeObjectForKey:@"cookie"];
    [self presentViewController:vc animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
